package com.goddess.yeecolor.activity.myFragmentItemActivityAll;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.LoginAcitivity;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.LogUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.wxPayActivity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.goddess.yeecolor.R.id.weixin_img;
import static com.goddess.yeecolor.R.id.weixin_ll;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/16
 * 作用: ******
 * 备注: 支付页面
 */
public class PaymentActivity extends BaseActivity {
    @InjectView(R.id.weixin_ll)
    RelativeLayout weixinLl;
    @InjectView(R.id.zhifubao_ll)
    RelativeLayout zhifubaoLl;
    @InjectView(weixin_img)
    ImageView weixinImg;
    @InjectView(R.id.zhifubao_img)
    ImageView zhifubaoImg;
    @InjectView(R.id.btn_join_submit)
    Button btnJoinSubmit;

    private TextView mTitle;
    private Toolbar mToobar;
    private String order_id;
    private TextView textView;
    private String price;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);

        ButterKnife.inject(this);
        broast();
        order_id = getIntent().getStringExtra("tag");
        initFindView();
        initData();
    }
    private MyBracastReceiver bracastReceiver;
    private void broast(){
        bracastReceiver=new MyBracastReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("com.zy.brocast");
        registerReceiver(bracastReceiver,filter);
    }
    @Override
    public void initOnClck() {
    }
    @Override
    public void initFindView() {
        mToobar = (Toolbar) findViewById(R.id.title_toobar);
        mTitle = (TextView) findViewById(R.id.title_content);
        mTitle.setText("支付");
        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view) -> {
            setFiish(this);
        });
        textView = (TextView) findViewById(R.id.payment_price);
    }

    @Override
    public void initData() {
        Map<Object, Object> map = new HashMap<>();
        map.put("order_id", order_id);
        map.put("unique", SharePreceUtils.getOther("user", "unique"));
        OkttpUtils.getIntence().OkttpPost(MyUrl.HUIYUAN, map, handler, 1);
    }

    private String jsonString;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString = msg.getData().getString("result");
                JSONObject object = new JSONObject(jsonString);
                int code = object.getInt("code");
                if (code == 100) {
                    JSONObject result = object.getJSONObject("result");
                    if (msg.what == 1) {
                        price = result.getString("price");
                        textView.setText(price+"元");

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };


    private boolean sf=true;
    @OnClick({weixin_ll, R.id.zhifubao_ll,R.id.btn_join_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case weixin_ll://微信支付单选按钮
                weixinImg.setImageDrawable(getResources().getDrawable(R.mipmap.radio2));
                zhifubaoImg.setImageDrawable(getResources().getDrawable(R.mipmap.radio1));
                sf=true;
                break;
            case R.id.zhifubao_ll://支付宝单选按钮
                zhifubaoImg.setImageDrawable(getResources().getDrawable(R.mipmap.radio2));
                weixinImg.setBackground(null);
                weixinImg.setImageDrawable(getResources().getDrawable(R.mipmap.radio1));
                sf=false;
                break;
            case R.id.btn_join_submit://提交按钮
                Intent intent=null;
                if(sf){
                    int prices = (int)(Double.parseDouble(price)*100);//价格
                    SharePreceUtils.saveString("orders","join","tag");
                    wxPayActivity.getIntance().pay(order_id,order_id+"",prices);
                }else{
                    intent = new Intent(PaymentActivity.this,LoginAcitivity.class);//支是选中支付宝的支付按钮
                    startActivity(intent);
                }

                break;
        }
    }

    class  MyBracastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String back=intent.getStringExtra("data");
            LogUtils.getInstence().info(back);
            finish();
            overridePendingTransition(0,0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(bracastReceiver);
    }
}
